package com.huahua.utils.util;

import com.huahua.utils.entity.FieldInfo;
import com.huahua.utils.entity.PropertyInfo;
import com.huahua.utils.entity.TableInfo;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.sql.SQLException;
import java.util.List;

public class Field2PropertyUtil {

    public static PropertyInfo fieldToProperty(FieldInfo fieldInfo) throws ClassNotFoundException {
        String fieldName = fieldInfo.getFieldName();
        PropertyInfo propertyInfo = new PropertyInfo();
        if (null == fieldName) {
            return null;
        }
        char[] chars = fieldName.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (c == '_') {
                int j = i + 1;
                if (j < chars.length) {
                    sb.append(StringUtils.upperCase(CharUtils.toString(chars[j])));
                    i++;
                }
            } else {
                sb.append(c);
            }
        }
        propertyInfo.setName(sb.toString());

        String fieldType = fieldInfo.getFieldType();
        propertyInfo.setSimpleType(Class.forName(fieldType).getSimpleName());


        return propertyInfo;
    }

    public static void main(String[] args) throws Exception {
        List<TableInfo> tableInfoList = JDBCUtil.getTableDetails();
        TableInfo tableInfo = tableInfoList.get(0);
        List<FieldInfo> fields = tableInfo.getFields();
        for (FieldInfo field : fields) {
            PropertyInfo propertyInfo = fieldToProperty(field);
            System.out.println(propertyInfo);
        }
    }


}
